import { getOrganizationInfoList  } from '@/api/organization'

export function setOrgOptions() {
      return getOrganizationInfoList().then(res => {
        if (res.code === 0) {
          const idMapping = res.data.reduce((acc, el, i) => {
            acc[el.orgId] = i;
            return acc;
          }, {});
          let root = null;
          res.data.forEach(el => {
  // 判断根节点
            if (el.orgPid === 0) {
                root = el;
                return;
            }
  // 用映射表找到父元素       
            var parentEl = res.data[idMapping[el.orgPid]];
  // 把当前元素添加到父元素的`children`数组中
            parentEl.children = [...(parentEl.children || []), el];
            });
            return root
        } 
        else {
          return null
        }
      })
    }

//   export function updateMemberOrgs(data) {
//     return request({
//       url: '/organization/updateMemberOrgs',
//       method: 'POST',
//       data: data
//     })
//   }
  